//
// Created by 党清浩 on 2021/10/12.
//

#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;

typedef struct Node {
    ElementType data;
    struct Node *next;
} Node, *LinkList;

/* 初始化单链表 */
void InitList( LinkList *linkList);
/* 创建单链表 */
void CreateFromTail(LinkList linkList);
/* 输出元素 */
void out(LinkList linkList);
/* 逆置 */
void ReverseList(LinkList linkList);

int main() {
    LinkList linkList;

    InitList(&linkList);

    CreateFromTail(linkList);
    printf("The sequence before the reverse:");
    out(linkList);
    /* 逆置函数 */
    ReverseList(linkList);
    printf("Inverse sequence:");
    out(linkList);
    return 0;
}
void InitList(LinkList *linkList) {
    *linkList = (LinkList) malloc(sizeof(Node));
    (*linkList)->next = NULL;
}

/*尾插法创建单链表*/
void CreateFromTail(LinkList linklist) {
    Node * r, * s;
    int flag = 1;
    r = linklist;
    while (flag) {
        int c;
        scanf("%d", &c);
        if (c != 0) {
            s = (Node *) malloc(sizeof(Node));
            s->data = c;
            r->next = s;
            r = s;
        } else {
            flag = 0;
            r->next = NULL;
        }
    }
}


void out(LinkList linklist) {
    Node *temp;
    temp = linklist->next;
    while (temp != NULL) {
        printf("%-4d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

void ReverseList(LinkList linkList) {
    Node * p ,* q;
    p = linkList->next;
    linkList->next = NULL;
    while (p != NULL) {
        q = p->next;
        p->next = linkList->next;
        linkList->next = p;
        p = q;
    }

}
